﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wintellect.LiveFx.Cloudboard
{
  public enum OptionsTab
  {
    General,
    ManageCloudboards
  }

  /// <summary>
  /// Interaction logic for Options.xaml
  /// </summary>
  public partial class Options : Window
  {
    private Configuration configuration;
    private CloudboardEntry defaultEntry;

    public Options(Configuration configuration) : this(configuration, OptionsTab.General)
    {
    }

    public Options(Configuration configuration, OptionsTab tab)
    {
      InitializeComponent();
      this.configuration = configuration;

      this.startOnLogin.IsChecked = this.configuration.StartOnLogin;
      this.checkForUpdates.IsChecked = this.configuration.CheckForUpdates;

      this.copyHotkeyEntry.Hotkey = this.configuration.CopyHotkey;

      this.uiHotkeyEntry.Hotkey = this.configuration.InsertHotkey;

      this.RefreshCloudboardList();
      this.configuration.ClipboardsUpdated += (o, e) => 
      { 
        Dispatcher.Invoke( new Action(() => { this.RefreshCloudboardList(); }));
      };

      switch (tab)
      {
        case OptionsTab.General:
        case OptionsTab.ManageCloudboards:
          this.optionTabs.SelectedIndex = (int)tab;
          break;
        default:
          throw new ArgumentOutOfRangeException("tab");
      }
    }

    private void RefreshCloudboardList()
    {
      IList<CloudboardEntry> entries = new List<CloudboardEntry>(from mo in this.configuration.Clipboards
                                             select new CloudboardEntry(
                                               mo,
                                               this.configuration.CurrentClipboard != null ? (mo.Title == this.configuration.CurrentClipboard.Name) : false));
      this.defaultEntry = (from e in entries where e.Default select e).FirstOrDefault();
      this.cloudBoards.ItemsSource = entries;
    }

    private void ok_Click(object sender, RoutedEventArgs e)
    {
      // General tab
      this.configuration.StartOnLogin = this.startOnLogin.IsChecked ?? false;
      this.configuration.CheckForUpdates = this.checkForUpdates.IsChecked ?? false;

      this.configuration.CopyHotkey = this.copyHotkeyEntry.Hotkey;

      this.configuration.InsertHotkey = this.uiHotkeyEntry.Hotkey;

      this.configuration.CurrentClipboard = new Clipboard(this.defaultEntry.Title);

      this.configuration.Save();

      this.Close();
    }

    private void cancel_Click(object sender, RoutedEventArgs e)
    {
      this.Close();
    }

    private void setDefault_Click(object sender, RoutedEventArgs e)
    {
      CloudboardEntry currentSelection = this.cloudBoards.SelectedValue as CloudboardEntry;
      if (currentSelection != null)
      {
        if (this.defaultEntry != null) this.defaultEntry.Default = false;
        currentSelection.Default = true;
        this.defaultEntry = currentSelection;
      }
    }

    private void add_Click(object sender, RoutedEventArgs e)
    {
      CloudboardDetails newEntryDialog = new CloudboardDetails(null);
      newEntryDialog.Owner = this;
      newEntryDialog.ShowDialog();
    }

    private void remove_Click(object sender, RoutedEventArgs e)
    {
      CloudboardEntry currentSelection = this.cloudBoards.SelectedValue as CloudboardEntry;
      if (currentSelection != null)
      {
        new Clipboard(currentSelection.Title).Delete();
      }
    }

    private void cloudBoards_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      CloudboardEntry currentSelection = this.cloudBoards.SelectedValue as CloudboardEntry;
      if (currentSelection != null)
      {
        this.setDefault.IsEnabled = !currentSelection.Default;
        this.remove.IsEnabled = true;
      }
      else
      {
        this.setDefault.IsEnabled = false;
        this.remove.IsEnabled = true;
      }
    }

    private void cloudBoards_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
      CloudboardEntry currentSelection = this.cloudBoards.SelectedValue as CloudboardEntry;
      if (currentSelection != null)
      {
        Clipboard clipboard = new Clipboard(currentSelection.Title);
        CloudboardDetails details = new CloudboardDetails(clipboard);
        details.Owner = this;
        details.ShowDialog();
      }
    }
  }
}
